package com.chengqj;

import com.chengqj.dao.OrderDO;
import com.chengqj.repository.OrderRepository;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * Copyright © 2020年 com.qxkj. All rights reserved.
 *
 * @author chengqj
 * TODO: 为了共建和谐社会，请为该类写点注释吧...
 * version 1.0.0 (接口必须带上)
 * @date 2020/11/6 15:32
 */
@Service
public class OrderService {
    @Autowired
    private OrderRepository orderRepository;
    @Transactional
    public String createCommit(OrderDO orderDO){
        orderRepository.save(orderDO);
        return orderDO.getId().toString();
    }
    @Transactional
    public String createRollback(OrderDO orderDO){
        orderRepository.save(orderDO);
        throw new RuntimeException();
    }

    public OrderDO getOrderById(ObjectId id){
        Optional<OrderDO> result = orderRepository.findById(id);
        if (result.isPresent()){
            return result.get();
        }else{
            return null;
        }
    }
}
